//
//  DatePickerView.m
//  YouOu
//
//  Created by weihong xuan on 2016/3/7.
//  Copyright © 2016年 weihong xuan. All rights reserved.
//

#import "DatePickerView.h"

//获取屏幕 宽度、高度
#define SCREEN_WIDTH                                  ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT                                 ([UIScreen mainScreen].bounds.size.height)
#define ScreenWithRateFor6                            SCREEN_WIDTH/375.0
#define TIME_COMPONENT  0
#define HOURS_COMPONENT      1
#define MINUTE_COMPONENT  2

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define Color_Background                     UIColorFromRGB(0xeeeeee)
#define Color_Moments_ColorRed               UIColorFromRGB(0xe9546b)
#define Color_AddAndReduce_BorderColor       UIColorFromRGB(0xcccccc)



@interface DatePickerView ()<UIPickerViewDataSource, UIPickerViewDelegate>

@end

@implementation DatePickerView
{
    UIView * _bgView;
    UIView * _whiteView;
    UIButton * _cancelBtn;
    UIButton * _commitBtn;
    
    NSArray * _areaDic;
    NSArray * _Time;
    NSArray * _Hours;
    NSArray * _Mintes;
    
    NSString *selectedProvince;
}

- (instancetype)init {
    if ( self = [super init]) {
        [self configViews];
    }
    return self;
}

- (void)configViews {
    _bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _bgView.alpha = 0.3;
    _bgView.backgroundColor = [UIColor blackColor];
    _bgView.hidden = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [_bgView addGestureRecognizer:tap];
    [[UIApplication sharedApplication].keyWindow addSubview:_bgView];
}

- (void)initBgWith:(NSString *)time {
    NSMutableArray * arrHour = [NSMutableArray array];
    for (int i = 0; i < 24; i ++) {
        [arrHour addObject:[NSString stringWithFormat:@"%02d",i]];
    }
    NSMutableArray * arrMintes = [NSMutableArray array];
    for (int i = 0; i < 60; i ++) {
        [arrMintes addObject:[NSString stringWithFormat:@"%02d",i]];
    }
    _areaDic = @[@[time],arrHour,arrMintes];
    
    _Time = _areaDic[TIME_COMPONENT];
    _Hours = _areaDic[HOURS_COMPONENT];
    _Mintes = _areaDic[MINUTE_COMPONENT];
    
//    _bgView = [[UIView alloc] initWithFrame:self.frame];
//    _bgView.alpha = 0.5;
//    _bgView.backgroundColor = [UIColor blackColor];
//    [self addSubview:_bgView];
    
    _whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 260, SCREEN_WIDTH, 260)];
    _whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_whiteView];
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    view.backgroundColor = Color_Background;
    [_whiteView addSubview:view];
    
    _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelBtn.frame = CGRectMake(15 * ScreenWithRateFor6, 0 * ScreenWithRateFor6, 50 * ScreenWithRateFor6, 40 * ScreenWithRateFor6);
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelBtn setTitleColor:Color_Moments_ColorRed forState:UIControlStateNormal];
    _cancelBtn.tag = 100 + 1;
    [_cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_cancelBtn];
    
    UILabel * lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 50, 0 * ScreenWithRateFor6, 100, 40 * ScreenWithRateFor6)];
    lblTitle.text = @"时间选择";
    lblTitle.textAlignment = NSTextAlignmentCenter;
    lblTitle.textColor = [UIColor blackColor];
    [view addSubview:lblTitle];
    
    _commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _commitBtn.frame = CGRectMake(SCREEN_WIDTH - 15 * ScreenWithRateFor6 - 50 * ScreenWithRateFor6, 0 * ScreenWithRateFor6, 50 * ScreenWithRateFor6, 40 * ScreenWithRateFor6);
    [_commitBtn setTitle:@"完成" forState:UIControlStateNormal];
    [_commitBtn setTitleColor:Color_Moments_ColorRed forState:UIControlStateNormal];
    _commitBtn.tag = 100 + 2;
    [_commitBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_commitBtn];
    
    _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 220)];
    _picker.dataSource = self;
    _picker.delegate = self;
    
    [_picker selectRow:0 inComponent:0 animated:YES];
    [_whiteView addSubview:_picker];
    
    UITapGestureRecognizer  * pin = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [_bgView addGestureRecognizer:pin];
    
}

- (void)btnClick:(UIButton *)btn {
    [self dismiss];
    if (self.dateBlock) {
        
        NSInteger cityIndex = [_picker selectedRowInComponent: HOURS_COMPONENT];
        NSInteger districtIndex = [_picker selectedRowInComponent: MINUTE_COMPONENT];
        
        NSString *cityStr = [_Hours objectAtIndex: cityIndex];
        NSString *districtStr = [_Mintes objectAtIndex:districtIndex];
        
        NSString *showMsg = [NSString stringWithFormat: @"%@:%@",cityStr, districtStr];
        self.dateBlock(btn.tag,showMsg);
    }
}

- (void)showWithTime:(NSString *)time {
    UIWindow * win = [[UIApplication sharedApplication].windows objectAtIndex:0];
    [win addSubview:self];
    self.frame = win.frame;
    self.center = CGPointMake(win.center.x, win.center.y * 2);
    [self initBgWith:time];
    
    [UIView beginAnimations:@"animan" context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:win cache:YES];
    [UIView animateWithDuration:10 animations:^{
        self.frame = win.frame;
//        _bgView.frame = self.frame;
        _bgView.hidden = NO;
    }];
    [UIView commitAnimations];
}

- (void)dismiss {
    UIWindow * win = [[UIApplication sharedApplication].windows objectAtIndex:0];
    [UIView beginAnimations:@"animaim1" context:nil];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:win cache:YES];
    [UIView animateWithDuration:2 animations:^{
        _whiteView.frame = CGRectMake(0, SCREEN_WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT);
//        _bgView.alpha = 0;
        _bgView.hidden = YES;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    [UIView commitAnimations];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return _areaDic.count;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == TIME_COMPONENT) {
        return [_areaDic[TIME_COMPONENT] count];
    }
    else if (component == HOURS_COMPONENT) {
        return [_areaDic[HOURS_COMPONENT] count];
    }
    else {
        return [_areaDic[MINUTE_COMPONENT] count];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == TIME_COMPONENT) {
        return [_Time objectAtIndex: row];
    }
    else if (component == HOURS_COMPONENT) {
        return [_Hours objectAtIndex: row];
    }
    else {
        return [_Mintes objectAtIndex: row];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == TIME_COMPONENT) {
        return 80;
    }
    else if (component == HOURS_COMPONENT) {
        return 100;
    }
    else {
        return 115;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *myView = nil;
    
    if (component == TIME_COMPONENT) {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 78, 15)];
        myView.textAlignment = NSTextAlignmentCenter;
        NSArray *arry = [[_Time objectAtIndex:row] componentsSeparatedByString:@" "];
        myView.text = [NSString stringWithFormat:@"%@",arry[0]];
        myView.font = [UIFont systemFontOfSize:17];
        myView.textColor = Color_AddAndReduce_BorderColor;
        myView.backgroundColor = [UIColor clearColor];
    }
    else if (component == HOURS_COMPONENT) {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 95, 15)];
        myView.textAlignment = NSTextAlignmentCenter;
        myView.text = [_Hours objectAtIndex:row];
        myView.font = [UIFont systemFontOfSize:17];
        myView.textColor = [UIColor blackColor];
        myView.backgroundColor = [UIColor clearColor];
    }
    else {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 110, 15)];
        myView.textAlignment = NSTextAlignmentCenter;
        myView.text = [_Mintes objectAtIndex:row];
        myView.font = [UIFont systemFontOfSize:17];
        myView.textColor = [UIColor blackColor];
        myView.backgroundColor = [UIColor clearColor];
    }
    
    return myView;
}

@end
